package com.jsmtr.rms.controller.DoZZCJ;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IDoGXService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.PhoneTools;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("/Do/ZZCJPhysical")
public class DoZZCJPhysicalController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IDoGXService doGXService;


    //[start] 查询毕业生信息

    //首页
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", request.getParameter("deptcode").trim());

            //名族
            model.addAttribute("minzu", utilService.optionList("B", "0"));
            //政治面貌
            model.addAttribute("zzmm", utilService.optionList("E", "0"));
            //最高学历
            model.addAttribute("education", utilService.optionList("F", "0"));
            //招聘地区
            model.addAttribute("regions", utilService.optionList("M", "0"));
            //招聘岗位
            List<ProjectInfoJobs> projectJobs=utilService.getProjectJobs(pid);
            model.addAttribute("jobs",projectJobs);

            List<BaseConfig> configs = utilService.getConfigs("dzmb");
            StringBuilder str1 = new StringBuilder();
            for (BaseConfig config : configs) {
                str1.append("<option value='" + config.getCont() + "'>" + config.getCont() + "</option>");
            }
            model.addAttribute("address", str1);

            List<String> dates = doGXService.getTjTimeList(pid);
            StringBuilder str2 = new StringBuilder();
            for (String date : dates) {
                if (date != null) {
                    str2.append("<option value='" + date + "'>" + date + "</option>");
                }
            }
            model.addAttribute("tjtime", str2);

            List<BaseConfig> _configs = utilService.getConfigs("dxmb");
            StringBuilder str3 = new StringBuilder();
            for (BaseConfig _config : _configs) {
                str3.append("<option value='" + _config.getCode() + "'>" + _config.getName() + "</option>");
            }
            model.addAttribute("dxmb", str3);

            //面试注意事项
            String tjzysx=utilService.getConfig("tjzysx");
            model.addAttribute("tjzysx",tjzysx);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/ZZCampusSpring/Physical/index";
    }

    /*@SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();
            user.setPath("/" + flowid + "/");

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());

            Map<String, Object> map = doGXService.getResumes(page, perpage, user, _map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                Object object = map.get("list");
                for (DUserResume _user : (List<DUserResume>) object) {
                    result.append("<tr><td><input type=\"checkbox\" name='uid' value='" + _user.getId() + "' id='cb_" + _user.getId() + "' /></td>");
                    result.append("<td>" + _user.getCode() + "</td>");
                    result.append("<td class='link'><a target='_blank' href='"
                            + request.getContextPath() + "/UserResume/ViewDResume?id="
                            + _user.getId() + "'>" + _user.getName()
                            + "</a></td>");//简历ID和HRGXUser的ID相同
                    *//*result.append("<td>" + _user.getCardaddress() + "</td>");*//*
                    result.append("<td>" + _user.getCardno() + "</td>");
                    result.append("<td>" + _user.getEducationname() + "</td>");
                    result.append("<td>" + (_user.getTjtime() == null ? "" : _user.getTjtime()) + "</td>");
                    result.append("<td>" + (_user.getTjaddress() == null ? "" : _user.getTjaddress()) + "</td>");
                    result.append("<td>" + (_user.getTjtime() == null ? "" : (_user.getTjlxr() + " " + _user.getTjlxdh())) + "</td>");
                    result.append("<td>" + _user.getTjmsg() + "</td>");
                    result.append("<td>" + _user.getTjstate() + "</td>");
                    result.append("<td>" + (_user.getTjremark() == null ? "" : _user.getTjremark()) + "</td>");

                    if (_user.getOp4() == null || _user.getOp4().equals("未公布")) {
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td class='false'><span>已公布</span></td>");
                    }

                    if (_user.getFqstate().equals("已放弃")
                            && _user.getFqfid().equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (_user.getTjstate().equals("未体检")) {
                            result.append("<td class='true'><span>未完成</span></td></tr>");
                        } else if (_user.getTjstate().equals("不合格")) {
                            result.append("<td class='false'><span>已完成</span></td></tr>");
                        } else {
                            boolean bl = _user.getFid().equals(flowid);
                            result.append("<td class='" + bl + "'><span>" + (bl == true ? "未完成" : "已完成") + "</span></td></tr>");
                        }
                    }
                }
            } else {
                result.append("<tr><td colspan='13'>无符合条件的数据</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='13'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }*/

    //region20190225改为
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();
            user.setPath("/" + flowid + "/");

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());

            Map<String, Object> map = doGXService.getResumesMap(page, perpage, user, _map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                List<HashMap> object = (List<HashMap>)map.get("list");
                for (HashMap<String,Object> _user :  object) {
                    result.append("<tr><td><input type='checkbox' id='cb_" + _user.get("ID") + "' value='" + _user.get("ID") + "' /></td>");
                    //result.append("<td>" + _user.get("CODE") + "</td>");

                    Integer cardcount= Integer.parseInt(_user.get("CARDCOUNT").toString());
                    if (cardcount>1){
                        result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + _user.get("ID") + "'>" + _user.get("NAME") + "</a>&nbsp;<span class='layui-badge'>"+cardcount+"</span></td>");
                    }else{
                        result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + _user.get("ID") + "'>" + _user.get("NAME") + "</a></td>");
                    }
                    result.append("<td>" + _user.get("CARDNO") + "</td>");
                    result.append("<td>" + _user.get("EDUCATIONNAME") + "</td>");
                    result.append("<td>" + (_user.get("TJTIME") == null ? "" : _user.get("TJTIME")) + "</td>");
                    result.append("<td>" + (_user.get("TJADDRESS") == null ? "" : _user.get("TJADDRESS")) + "</td>");
                    result.append("<td>" + (_user.get("TJLXR")==null?"": _user.get("TJLXR")+ "" + _user.get("TJLXDH")==null?"": _user.get("TJLXDH"))+"</td>");
                    result.append("<td>" + _user.get("TJMSG") + "</td>");
                    result.append("<td>" + _user.get("TJSTATE") + "</td>");
                    result.append("<td>" + (_user.get("TJREMARK") == null ? "" : _user.get("TJREMARK")) + "</td>");

                    if (_user.get("OP4") == null || _user.get("OP4").equals("未公布")) {
                        result.append("<td></td>");
                        result.append("<td><input type='hidden' value='"+_user.get("MID")+"'>"+ (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td>"+ (_user.get("LOGINTIME")==null?"":_user.get("LOGINTIME").toString())+"</td>");
                        result.append("<td><input type='hidden' value='"+_user.get("MID")+"'>"+ (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='false'><span>已公布</span></td>");
                    }
                    result.append("<td>");
                    if (_user.get("FQSTATE").equals("已放弃") && _user.get("FQFID").equals(flowid)) {
                        result.append("<span class='layui-badge layui-bg-gray' title='已放弃'>弃</span>");
                        //result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {

                            if (_user.get("TJSTATE") == null) {
                                //result.append("<td class='true'><span>未完成</td></tr>");
                                result.append("<span class='layui-badge layui-bg-orange' title='未完成'>未</span>");
                            } else {
                                if (_user.get("TJSTATE").equals("不合格")||_user.get("TJSTATE").equals("未通过")||_user.get("TJSTATE").equals("不通过")||_user.get("TJSTATE").equals("未参加")) {
                                    //result.append("<td class='false'><span>已完成</td></tr>");
                                    result.append("<span class='layui-badge layui-bg-green' title='已完成'>完</span>");
                                } else {
                                    if (_user.get("FID").equals(flowid)) {
                                        //result.append("<td class='true'><span>未完成</td></tr>");
                                        result.append("<span class='layui-badge layui-bg-orange' title='未完成'>未</span>");
                                    } else {
                                        //result.append("<td class='false'><span>已完成</td></tr>");
                                        result.append("<span class='layui-badge layui-bg-green' title='已完成'>完</span>");
                                    }
                                }
                            }

                    }
                    if (_user.get("NRID") != null) {
                        result.append("<span class='layui-badge layui-bg-blue' title='转岗(原岗位:"+_user.get("NJNAME")+")'>岗</span>");
                        //result.append("<td class='ytj'><span title='" + _user.get("NJNAME") + "'>已推荐</span></td></tr>");
                    }
                    result.append("</td>");
                    result.append("</tr>");
                }
            } else {
                result.append("<tr><td colspan='15'>无符合条件的数据</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='15'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }
    //endregion

    //[end]

    //[start] 体检安排

    @RequestMapping(value = "/PlanAction", method = RequestMethod.POST)
    public void PlanAction(HttpServletRequest request, HttpServletResponse response, DUserResume _user) {
        try {
            String[] uids = request.getParameterValues("uid");
            if (uids == null) {
                utilService.printJsonResult(false, "请选择参加体检的应聘者!", "no", response);
            } else {
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String tjzysx=request.getParameter("tjzysx");
                String pid=request.getParameter("pid");
                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> users = doGXService.getResumes(uids);
                        List<DoZysx> zysxList=new ArrayList<>();
                        for (DUserResume user : users) {
                            if (user.getFqstate().equals("未放弃")
                                    && user.getFid().equals(fid)
                                    && user.getTjmsg().equals("未发通知")) {
                                user.setTjtime(_user.getTjtime());
                                user.setTjaddress(_user.getTjaddress());
                                user.setTjlxr(_user.getTjlxr());
                                user.setTjlxdh(_user.getTjlxdh());
                                editlist.add(user);
                            }

                            if(tjzysx!=null&&tjzysx.trim().length()>0){
                               DoZysx userZysx= utilService.getUserZysx(pid,fid,user.getId());
                                DoZysx zysx=null;
                               if (userZysx==null){
                                    zysx=new DoZysx();
                                    zysx.setId(UUID.randomUUID().toString());
                                   zysx.setFid(fid);
                                   zysx.setPid(pid);
                                   zysx.setRemark(tjzysx);
                                   zysx.setUserid(user.getId());
                                   zysx.setType("TJ");
                                   zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                   zysx.setAdduser(cuser.getUcode());
                                   zysxList.add(zysx);
                               }else{
                                   zysx=userZysx;
                                   zysx.setRemark(tjzysx);
                                   zysx.setType("TJ");
                                   zysx.setAddtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                   zysx.setAdduser(cuser.getUcode());
                                   editlist.add(zysx);
                               }

                            }
                        }

                        if (editlist.size() > 0) {
                            utilService.editList(editlist);
                            if(zysxList.size()>0) {
                                utilService.addList(zysxList);
                            }
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败!", "no", response);
        }
    }

    //[end]

    //[start] 发布体检通知

    @RequestMapping(value = "/MsgAction", method = RequestMethod.POST)
    public void MsgAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] uids = request.getParameterValues("uid");
            if (uids == null) {
                utilService.printJsonResult(false, "请选择参加体检的应聘者!", "no", response);
            } else {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码

                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List<DUserResume> editlist = new ArrayList<DUserResume>();
                        String dxnr = request.getParameter("dxnr").trim();

                        List<Map<String, String>> msgs = new ArrayList<Map<String, String>>();

                        List<DUserResume> users = doGXService.getResumes(uids);

                        for (DUserResume user : users) {
                            if (/*user.getTjmsg().equals("未发通知")
                                    &&*/ user.getFid().equals(fid)
                                    && user.getFqstate().equals("未放弃")
                                    && user.getTjtime() != null) {
                                user.setTjmsg("已发通知");
                                editlist.add(user);

                                //-------------短信----------------
                                Map<String, String> _map = new HashMap<String, String>();
                                _map.put("tel", user.getTel());
                                if(dxnr.indexOf("{姓名}")>0){
                                    _map.put("cont", dxnr.replace("{姓名}", user.getName()).replace("{时间}", user.getTjtime()).replace("{地点}", user.getTjaddress()));
                                }else{
                                    _map.put("cont", dxnr);
                                }

                                msgs.add(_map);
                            }
                        }
                        if (editlist.size() > 0) {
                            utilService.editList(editlist);
                            BaseMessageConfig messageConfig=utilService.getMessageConfig();
                            //发送短信
                            if (PhoneTools.sendMsg(msgs,messageConfig.getUsername(),messageConfig.getPassword(),messageConfig.getHost())) {
                                utilService.printJsonResult(true, "操作成功！", "no", response);
                            } else {
                                utilService.printJsonResult(false, "短信发送失败！", "no", response);
                            }
                        } else {
                            utilService.printJsonResult(false, "当前信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 导出体检人员信息

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/DownAction", method = RequestMethod.GET)
    public void DownAction(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        response.setContentType("application/vnd.ms-excel");
        String codedFileName = null;
        OutputStream fOut = null;
        try {
            codedFileName = java.net.URLEncoder.encode("体检人员信息", "UTF-8");
            response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
            response.setCharacterEncoding("iso-8859-1");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();

            //[start] 表头样式
            HSSFCellStyle headerStyle = workbook.createCellStyle();
            //创建字体的对象
            HSSFFont font = workbook.createFont();
            //将字体加粗
            font.setBold(true);
            //垂直居中
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置底边框;
            headerStyle.setBorderBottom(BorderStyle.THIN);
            //设置底边框颜色;
            headerStyle.setBottomBorderColor(IndexedColors.BLACK.index);
            //设置左边框;
            headerStyle.setBorderLeft(BorderStyle.THIN);
            //设置左边框颜色;
            headerStyle.setLeftBorderColor(IndexedColors.BLACK.index);
            //设置右边框;
            headerStyle.setBorderRight(BorderStyle.THIN);
            //设置右边框颜色;
            headerStyle.setRightBorderColor(IndexedColors.BLACK.index);
            //设置顶边框;
            headerStyle.setBorderTop(BorderStyle.THIN);
            //设置顶边框颜色;
            headerStyle.setTopBorderColor(IndexedColors.BLACK.index);
            //将新设置的字体属性放置到样式中
            headerStyle.setFont(font);

            //[end]

            //[start] 表格内容样式

            HSSFCellStyle bodyStyle = workbook.createCellStyle();
            //创建字体的对象
            HSSFFont _font = workbook.createFont();
            //垂直居中
            bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置底边框;
            bodyStyle.setBorderBottom(BorderStyle.THIN);
            //设置底边框颜色;
            bodyStyle.setBottomBorderColor(IndexedColors.BLACK.index);
            //设置左边框;
            bodyStyle.setBorderLeft(BorderStyle.THIN);
            //设置左边框颜色;
            bodyStyle.setLeftBorderColor(IndexedColors.BLACK.index);
            //设置右边框;
            bodyStyle.setBorderRight(BorderStyle.THIN);
            //设置右边框颜色;
            bodyStyle.setRightBorderColor(IndexedColors.BLACK.index);
            //设置顶边框;
            bodyStyle.setBorderTop(BorderStyle.THIN);
            //设置顶边框颜色;
            bodyStyle.setTopBorderColor(IndexedColors.BLACK.index);
            //将新设置的字体属性放置到样式中
            bodyStyle.setFont(_font);

            //[end]

            //[start] 笔试安排表头

            HSSFRow headrow = sheet.createRow(0);//创建一行
            headrow.setHeight((short) 400);

            HSSFCell headcel0 = headrow.createCell(0);
            headcel0.setCellValue(new HSSFRichTextString("编号"));
            headcel0.setCellStyle(headerStyle);

            HSSFCell headcel1 = headrow.createCell(1);
            headcel1.setCellValue(new HSSFRichTextString("姓名"));
            headcel1.setCellStyle(headerStyle);

            HSSFCell headcel2 = headrow.createCell(2);
            headcel2.setCellValue(new HSSFRichTextString("性别"));
            headcel2.setCellStyle(headerStyle);

            HSSFCell headcel3 = headrow.createCell(3);
            headcel3.setCellValue(new HSSFRichTextString("证件号码"));
            headcel3.setCellStyle(headerStyle);

            HSSFCell headcel4 = headrow.createCell(4);
            headcel4.setCellValue(new HSSFRichTextString("专业"));
            headcel4.setCellStyle(headerStyle);

            HSSFCell headcel5 = headrow.createCell(5);
            headcel5.setCellValue(new HSSFRichTextString("体检标准"));
            headcel5.setCellStyle(headerStyle);

            HSSFCell headcel6 = headrow.createCell(6);
            headcel6.setCellValue(new HSSFRichTextString("体检结果"));
            headcel6.setCellStyle(headerStyle);

            sheet.setColumnWidth(0, 0);
            sheet.setColumnWidth(1, 3000);
            sheet.setColumnWidth(2, 2000);
            sheet.setColumnWidth(3, 6000);
            sheet.setColumnWidth(4, 6000);
            sheet.setColumnWidth(5, 6000);
            sheet.setColumnWidth(6, 6000);


            //[end]

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());

            Map<String, Object> map = doGXService.getTjResumes(user, _map);
            Object object = map.get("list");

            int i = 1;
            for (Object[] _user : (List<Object[]>) object) {
                HSSFRow row = sheet.createRow(i);//创建一行
                row.setHeight((short) 400);
                for (int j = 0; j < 7; j++) {
                    HSSFCell cell = row.createCell(j);//创建一列
                    cell.setCellStyle(bodyStyle);
                   // cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellType(CellType.STRING);
                    if (j < 5)
                        cell.setCellValue(new HSSFRichTextString(_user[j] == null ? "" : _user[j].toString()));
                    else
                        cell.setCellValue(new HSSFRichTextString(""));
                }
                i++;
            }

            fOut = response.getOutputStream();
            workbook.write(fOut);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fOut.flush();
                fOut.close();
            } catch (IOException e) {
            }
        }
    }

    //[end]

    //[start] 获得短信模版内容

    @RequestMapping("/getMsg")
    public void getMsgCont(HttpServletRequest request, HttpServletResponse response) {
        try {
            String msg = utilService.getConfig(request.getParameter("code").trim());
            utilService.printTextResult(msg, response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("", response);
        }
    }

    //[end]

    //[start] 提交体检结果

    @RequestMapping(value = "/RAction", method = RequestMethod.POST)
    public void RAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] uids = request.getParameterValues("uid");
            if (uids == null) {
                utilService.printJsonResult(false, "请选择人员！", "no", response);
            } else {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                String tjstate = request.getParameter("tjstate").trim();//体检结果
                String tjremark = request.getParameter("tjremark").trim();//体检备注

                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    if (utilService.FlowIsDoing(fid)) {
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> users = doGXService.getResumes(uids);
                        for (DUserResume user : users) {
                            if (user.getFqstate().equals("未放弃")
                                    && user.getFid().equals(fid)
                                    && user.getTjmsg().equals("已发通知")
                                    && !tjstate.equals(user.getTjstate())) {
                                user.setTjdate(new Date());
                                user.setTjremark(tjremark);
                                user.setTjstate(tjstate);
                                user.setTjuid(cuser.getId());
                                user.setTjuname(cuser.getUname());

                                editList.add(user);
                            }
                        }

                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中信息不支持此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }

    //[end]

    //[start] 流程扭转到下一步

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> users = doGXService.getResumes(ids);
                        ProjectInfoFlow nFlow = doGXService.getNextFlow(pid, deptcode, fid);

                        for (DUserResume user : users) {
                            if (user.getFid().equals(fid) && user.getFqstate().equals("未放弃")) {
                                if("合格".equals(user.getTjstate())||"已合格".equals(user.getTjstate())||"通过".equals(user.getTjstate())||"已通过".equals(user.getTjstate())){
                                    user.setFid(nFlow.getId());
                                    user.setPath(user.getPath() + nFlow.getId() + "/");
                                    editlist.add(user);
                                }

                            }
                        }

                        if (editlist.size() > 0) {
                            nFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                            editlist.add(nFlow);

                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]
}
